package com.key.service.impl;

import com.github.pagehelper.PageHelper;
import com.key.common.R;
import com.key.common.TranException;
import com.key.common.dto.UserAssetsDTO;
import com.key.entity.*;
import com.key.mapper.IndexAssetsMapper;
import com.key.service.IndexAssetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @Author: li
 * @CreateTime: 2023-02-20  15:19
 */
@Service
public class IndexAssetsServiceImpl implements IndexAssetsService {

    @Autowired
    IndexAssetsMapper indexAssetsMapper;

    @Override
    public R showUserAssetsInfo(Integer currentPage, Integer pageSize,Integer assetsManagerId) {
        R r = new R();
        try {
            PageHelper.startPage(currentPage, pageSize);
            List<UserAssetsDTO> assetsList = indexAssetsMapper.selectUserAssetsInfo(assetsManagerId);
            r.setCode(200);
            r.setMsg("success");
            r.setData(assetsList);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }


        return r;
    }

    @Override
    public R showAssetsInCompany(Integer assetsManagerId) {
        R r = new R();

        try {
            Company company = indexAssetsMapper.selectAssetsInCompany(assetsManagerId);
            r.setCode(200);
            r.setMsg("success");
            r.setData(company);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }

        return r;
    }

    @Override
    public R showAllCompany() {
        R r = new R();
        try {
            List<Company> companyList = indexAssetsMapper.selectAllCompany();
            r.setCode(200);
            r.setMsg("success");
            r.setData(companyList);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }

        return r;
    }

    @Override
    public R showAllDepartmentByCompanyId(Integer companyId) {
        R r = new R();

        try {
            List<Department> departmentList = indexAssetsMapper.selectAllDepartmentByCompanyId(companyId);
            r.setCode(200);
            r.setMsg("success");
            r.setData(departmentList);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    @Override
    public R showPersonByDeptId(Integer departmentId) {
        R r = new R();

        try {
            List<SysUser> userList = indexAssetsMapper.selectUserByDepartmentId(departmentId);
            r.setCode(200);
            r.setMsg("success");
            r.setData(userList);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    @Override
    public R showUserOperationalAssets(Integer assetsManagerId) {

        R r = new R();

        try {
            List<UserAssetsDTO> assetsList = indexAssetsMapper.selectUserOperationalAssets(assetsManagerId);
            r.setCode(200);
            r.setMsg("success");
            r.setData(assetsList);
            System.out.println("-----------");
            System.out.println(assetsList);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }


        return r;
    }

    @Override
    @Transactional
    public R addAssetsAllocation(AssetsAllocation allocation, Integer[] ids) {

        System.out.println("==============");
        System.out.println(ids);

      //  String[] id = ids.split(",");

     //   Integer[] integers = new Integer[id.length];

//        for(int i=0;i<id.length;i++){
//            integers[i]=Integer.parseInt(id[i]);
//        }
//        System.out.println(integers);



        R r = new R();

        try {
            //添加调拨信息单
            int i = indexAssetsMapper.insertAssetsAllocation(allocation);





            for(Integer assetsId:ids){
                HashMap<String, Object> map = new HashMap<>();
                map.put("allocationNum",allocation.getAllocationNum());
                map.put("assetsId",assetsId);
                //添加调拨信息单和资产的关系表
                int i1 = indexAssetsMapper.insertAssetsAllocationRelation(map);

            }

            //修改资产状态
            for (Integer AssetsId:ids){
                int i1 = indexAssetsMapper.updateAssetsState(AssetsId);


            }

            if (i>0) {

                r.setCode(200);
                r.setMsg("添加成功");
                return r;
            }
            else {
                r.setCode(500);
                r.setMsg("添加失败");
                return r;
            }
        } catch (Exception e) {

           throw new TranException("事务异常");

        }



    }
}
